var app = angular.module('queryApp', []);

app.controller('queryController', function($scope, $http, QueryService) {
	$scope.query;
	$scope.view=function(){
		$http.get(timestamp('qz/channel/getAll/')).success(function(response){
    		$scope.channelList=response.data;
    		$("#channelId").select2({
    			 placeholder: "请选择支付机构",
    			 allowClear: true
    		});
        });
    		
    	$http.get(timestamp('qz/business/getAll/')).success(function(response){
    		$scope.businessList=response.data;
    		$("#businessId").select2({
    			 placeholder: "请选择业务分类",
    			 allowClear: true
    		});
        });    	
    	
    	$http.get(timestamp('sys/dic/getByGroup/zfksx')).success(function(response){
    		$scope.interPropList=response.data;				 
        });
    	
    	$scope.interProps = [];	
        // toggle selection for a given interProps by id
        $scope.toggleSelection = function toggleSelection(propId) {
        	var idx = $scope.interProps.indexOf(propId);
        	// is currently selected
       
        	if (idx > -1) {
        		$scope.interProps.splice(idx, 1);
        	}else {
        		// is newly selected
        		$scope.interProps.push(propId);
        	}	    
        };
    	
    	$scope.query = {};
	}
	
    $scope.load=function(isRest){    
		$scope.query=$scope.query?$scope.query:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.query.pageNum=pageNum;
		$scope.query.pageSize=pageSize;
		
		var param={};
		for(var k in $scope.query){
			param[k] = $scope.query[k];
		} 
		if($scope.interProps!=null && $scope.interProps!=''){
			param.interProps=$scope.interProps;			
		}				
				
		QueryService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
		});
	} 
	
    $scope.view();
	$scope.load();
	$scope=pagination.init($scope);
	
	$scope.refreshData=function(){
		$scope.load();
		 
	};
	
	$scope.queryReset=function(){
		$scope.view();
	};
 
	$scope.listChan=function(){ 
		load();
		 
	}; 		
});
	 
app.factory('QueryService', function ($http) {
	var list = function (param) {
		return $http.post('qz/query/getByPage',param);
	};
	return {
		list:function(param){
			return list(param);
		}
	}
});